package com.zkingsoft.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.web.bean.BusParameterSettings;
import com.zkingsoft.web.service.BusParameterSettingsService;
import com.zkingsoft.web.tool.SystemConstance;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-11-28 15:14
 */
@Controller
@RequestMapping(value = "admin/busParameterSettings")
public class BusParameterSettingsController {

	@Resource
	private BusParameterSettingsService busParameterSettingsService;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "BusParameterSettings_BEV";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(BusParameterSettings busParameterSettings, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		busParameterSettings.setCompanyId(user.getCompanyId());
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS,
				busParameterSettingsService.findInPage(busParameterSettings, pageVo),
				busParameterSettingsService.findTotal(busParameterSettings));
		return result;
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(BusParameterSettings busParameterSettings) {
		if (busParameterSettings.getParamId() != null) {
			int i = busParameterSettingsService.modifyByMap(WebUtil.getSessionAttribute(BEV), busParameterSettings);
			WebUtil.removeSessionAttribute(BEV);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL, "修改失败");
			}
		} else {
			SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
			busParameterSettings.setCompanyId(user.getCompanyId());
			int i = busParameterSettingsService.add(busParameterSettings);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS, "新增成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL, "新增失败");
			}
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		BusParameterSettings busParameterSettings;
		if (id != null) {
			busParameterSettings = busParameterSettingsService.findById(id);
			WebUtil.getRequest().setAttribute("obj", busParameterSettings);
			WebUtil.setSessionAttribute(BEV, busParameterSettings);
		}
		return "admin/bus/parameterSetting-form";
	}

	/**
	 * 删除
 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = busParameterSettingsService.remove(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "删除失败");
		}
	}
	
	/**
	 * 批量修改web配置
	 */
	@RequestMapping(value = "/modifyWebParam")
	public @ResponseBody AjaxResult modifyWebParam(ParamVo paramVo) {
		for (BusParameterSettings param : paramVo.getParam()) {
			busParameterSettingsService.modifyByModel(param);
			// 同时更新session
			WebUtil.setSessionAttribute(param.getParamValue2(), param.getParamValue());
		}
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, "更新成功");
	}

	/**
	 * 加入批量修改web配置
	 */
	@RequestMapping(value = "/toWebParam")
	public String toWebParam(Long id) {
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		BusParameterSettings query = new BusParameterSettings();
		query.setParamValue1(SystemConstance.CMSPZ);
		query.setCompanyId(user.getCompanyId());
		List<BusParameterSettings> list = busParameterSettingsService.findByModel(query);
		WebUtil.getRequest().setAttribute("list", list);
		return "admin/web/webSet-form";
	}

}